﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RPTFactoryCertificate : BasePage
    {
        string wholSequence = "LT/S实-12-01";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }
        #region Private Method

        private void BindGridView()
        {
            RPTFactoryCertificateService RPTFactoryCertificateServiceObj = ServiceFactory.getRPTFactoryCertificateService();
            OperationResult<List<tb_RPT_FactoryCertificate>> result = RPTFactoryCertificateServiceObj.GetAllFactoryCertificate();

            if (result.operationSuccess)
            {
                gvFactoryCertificateList.DataSource = result.obj;
                gvFactoryCertificateList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            RPTFactoryCertificateService RPTFactoryCertificateServiceObj = ServiceFactory.getRPTFactoryCertificateService();
            OperationResult<tb_RPT_FactoryCertificate> resultEdit = RPTFactoryCertificateServiceObj.GetFactoryCertificateById(id);


            if (resultEdit.operationSuccess == true)
            {

                txtEditProjectName.Text = resultEdit.obj.ProjectName;
                txtEditCastingParts.Text = resultEdit.obj.CastingParts;
                txtEditBuyer.Text = resultEdit.obj.Buyer;
                txtEditConcreteGrade.Text = resultEdit.obj.ConcreteGrade;
                txtEditMixNumbers.Text = resultEdit.obj.MixNumbers;
                txtEditCementVarieties.Text = resultEdit.obj.CementVarieties;
                txtEditDeliveryDate.Text = resultEdit.obj.DeliveryDate;
                txtEditSupplier.Text = resultEdit.obj.Supplier;
                txtEditFactorySlump.Text = resultEdit.obj.FactorySlump;
                txtEditVolume.Text = resultEdit.obj.Volume;
                txtEditCementManufacturers.Text = resultEdit.obj.CementManufacturers;
                txtEditResult.Text = resultEdit.obj.Result;
                txtEditReview.Text = resultEdit.obj.Review;
                txtEditApproval.Text = resultEdit.obj.Approval;
                txtEditTest.Text = resultEdit.obj.Test;

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion

        #region Event

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RPTFactoryCertificateService RPTFactoryCertificateServiceObj = ServiceFactory.getRPTFactoryCertificateService();
            tb_RPT_FactoryCertificate instance = new tb_RPT_FactoryCertificate();

            instance.Sequence = wholSequence; 
            instance.ProjectName = txtProjectName.Text;
            instance.CastingParts = txtCastingParts.Text;
            instance.Buyer = txtBuyer.Text;
            instance.ConcreteGrade = txtConcreteGrade.Text;
            instance.MixNumbers = txtMixNumbers.Text;
            instance.CementVarieties = txtCementVarieties.Text;
            instance.DeliveryDate = txtDeliveryDate.Text;
            instance.Supplier = txtSupplier.Text;
            instance.FactorySlump = txtFactorySlump.Text;
            instance.Volume = txtVolume.Text;
            instance.CementManufacturers = txtCementManufacturers.Text;
            instance.Result = txtResult.Text;
            instance.Review = txtReview.Text;
            instance.Approval = txtApproval.Text;
            instance.Test = txtTest.Text;
             

            OperationResult<tb_RPT_FactoryCertificate> result = RPTFactoryCertificateServiceObj.CreateFactoryCertificate(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            RPTFactoryCertificateService RPTFactoryCertificateServiceObj = ServiceFactory.getRPTFactoryCertificateService();
            tb_RPT_FactoryCertificate entity = new tb_RPT_FactoryCertificate();

            entity.Id = editId;
            entity.Sequence = wholSequence;
            entity.ProjectName = txtEditProjectName.Text;
            entity.CastingParts = txtEditCastingParts.Text;
            entity.Buyer = txtEditBuyer.Text;
            entity.ConcreteGrade = txtEditConcreteGrade.Text;
            entity.MixNumbers = txtEditMixNumbers.Text;
            entity.CementVarieties = txtEditCementVarieties.Text;
            entity.DeliveryDate = txtEditDeliveryDate.Text;
            entity.Supplier = txtEditSupplier.Text;
            entity.FactorySlump = txtEditFactorySlump.Text;
            entity.Volume = txtEditVolume.Text;
            entity.CementManufacturers = txtEditCementManufacturers.Text;
            entity.Result = txtEditResult.Text;
            entity.Review = txtEditReview.Text;
            entity.Approval = txtEditApproval.Text;
            entity.Test = txtEditTest.Text;

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RPT_FactoryCertificate> resultEdit = RPTFactoryCertificateServiceObj.ModifyFactoryCertificate(entity);

            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RPTFactoryCertificateService RPTFactoryCertificateServiceObj = ServiceFactory.getRPTFactoryCertificateService();
            tb_RPT_FactoryCertificate deleteEntity = new tb_RPT_FactoryCertificate();

            deleteEntity.Id = deleteId;
            RPTFactoryCertificateServiceObj.DeleteFactoryCertificate(deleteEntity);

            BindGridView();
        }

        protected void gvFactoryCertificateList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvFactoryCertificateList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvFactoryCertificateList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
